package com.rjt.article.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.rjt.article.model.ArticleContentModel;
import com.rjt.article.model.ArticleModel;
import com.rjt.article.model.ArticleNumModel;
import com.rjt.article.repository.ArticleContentRepository;
import com.rjt.article.repository.ArticleNumRepository;
import com.rjt.article.repository.ArticleRepository;
import com.rjt.article.service.ArticleService;
import com.rjt.article.vo.ArticleVo;
import com.rjt.common.util.StaticMethod;
import com.rjt.spider.SpiderUtil;
import com.rjt.user.service.UserService;
@Service
public class ArticleServiceImpl implements ArticleService{

	@Autowired
	private ArticleRepository articleRepository;
	@Autowired
	private ArticleContentRepository articleContentRepository;
	@Autowired
	private ArticleNumRepository articleNumRepository;
	@Autowired
	private UserService userService;
	
	@Value("${UEditor.picPath}")
	private String picPath;
	@Value("${UEditor.hostUrl}")
	private String hostUrl;
	
	@Override
	public ArticleModel findById(String id) {
		// TODO Auto-generated method stub
		return articleRepository.findOne(id);
	}
	@Override
	@Transactional
	public String add(ArticleVo articleVo,String method) {
		// TODO Auto-generated method stub
		String flag="success";
		String id = "mw"+RandomStringUtils.randomAlphanumeric(6);
		Date date = new Date();
		ArticleModel article=new ArticleModel();
		ArticleContentModel articleContent=new ArticleContentModel();
		ArticleNumModel articleNum=new ArticleNumModel();
		BeanUtils.copyProperties(articleVo, article);
		BeanUtils.copyProperties(articleVo, articleContent);
		BeanUtils.copyProperties(articleVo, articleNum);
		if(Objects.equals(method, "edit")) {
			id = articleVo.getId();
			date=articleRepository.getOne(id).getCreateDate();
		}
		article.setId(id);
		article.setuId(userService.getuId());
		article.setLink(hostUrl+"/"+id);
		article.setCreateDate(date);
		articleContent.setId(id);
		articleContent.setCreateDate(date);
		articleNum.setId(id);
		
		try {
			articleContent.setContent(SpiderUtil.handImg(articleContent.getContent(),picPath, hostUrl));
			articleRepository.save(article);
			articleContentRepository.save(articleContent);
			articleNumRepository.save(articleNum);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			flag="error";
		}
		return flag;
	}
	@Override
	public  Page<ArticleModel> getList(HttpServletRequest request) {
		// TODO Auto-generated method stub
		//从0开始的
		int curPage = StaticMethod.nullObject2int(request.getParameter("page")) <= 0 ? 1 : 
			StaticMethod.nullObject2int(request.getParameter("page"));
		int pageSize = StaticMethod.nullObject2int(request.getParameter("limit")) <= 0 ? 10 : 
			StaticMethod.nullObject2int(request.getParameter("limit"));
		 PageRequest pageRequest = new PageRequest(curPage-1, pageSize,Direction.DESC, "createDate");
		 ArticleModel articleModel = new ArticleModel();
		 articleModel.setuId(userService.getuId());
		 Example<ArticleModel> example = Example.of(articleModel);
		 return articleRepository.findAll(example,pageRequest);
		
	}
	@Override
	public String delete(List<ArticleModel> articles) {
		// TODO Auto-generated method stub
		String flag="success";
		try {
			articles.forEach(model->{
				articleRepository.delete(model.getId());
				articleContentRepository.delete(model.getId());
				articleNumRepository.delete(model.getId());
			});
		} catch (Exception e) {
			// TODO: handle exception
			flag="error";
		}
		
		return flag;
	}
	@Override
	public ArticleVo findArticle(String id) {
		// TODO Auto-generated method stub
		ArticleModel articleModel = articleRepository.findOne(id);
		ArticleContentModel articleContentModel = articleContentRepository.findOne(id);
		ArticleNumModel articleNumModel = articleNumRepository.findOne(id);
		ArticleVo articleVo = new ArticleVo();
		BeanUtils.copyProperties(articleModel, articleVo);
		BeanUtils.copyProperties(articleContentModel, articleVo);
		BeanUtils.copyProperties(articleNumModel, articleVo);
		return articleVo;
	}
	@Override
	public ArticleVo parseUrl(String weixinUrl) {
		// TODO Auto-generated method stub
		return SpiderUtil.initArticleVo(weixinUrl, picPath, hostUrl);
	}

}
